﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.Library.Web;
using ECommerce.Library.DataStructure;
using Entity;
using ECommerce.Library;
using AutoGear.BLL;

public partial class myaccount_product_wishlist : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                if (SessionManager.ReturnUrl != null)
                {
                    string returnUrl = SessionManager.ReturnUrl;
                    SessionManager.ReturnUrl = null;
                    Response.Redirect(returnUrl);
                }
                SetPageTitleAndBreadcramb();
                LoadGrid();
            }
            else
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Login : "~/partner/" + SupplierId + "/login.aspx");
            }
        }
    }


    private void SetPageTitleAndBreadcramb()
    {
        ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

        Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
        Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

        if (litPageTitle != null)
        {
            litCurrentPage.Text = litPageTitle.Text = "Product Wish List";

        }
    }

    private void LoadGrid()
    {
        ECommerce.DAL.CustomCollection<TblProductWishList> data = new ECommerce.DAL.CustomCollection<TblProductWishList>();

        data = TblProductWishListExtended.GetTblProductWishListBySystemUsertId(SessionManager.LoggedUser.UserId);

        ECommerce.DAL.CustomCollection<TblProduct> product = new ECommerce.DAL.CustomCollection<TblProduct>();


        if (data != null && data.Count > 0)
        {
            string list = string.Empty;
            for (int i = 0; i < data.Count; i++)
            {
                TblProductWishList userwish = data[i];
                list += i == 0 ? "( " : "";
                list += userwish.ProductId.ToString();
                list += i == data.Count - 1 ? ")" : ",";
            }
            if (!string.IsNullOrEmpty(list))
            {
                string queryCondition = " ProductId IN " + list;
                product = TblProductExtended.GetBackendProductListByCondition(queryCondition, "DisplayOrder",0,10);
                
            }
          
        }

        GridViewProductWishList.DataSource = product;
        GridViewProductWishList.DataBind();
       
    }

    protected void GridViewProductWishList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmdDeleteList")
        {
            System.Collections.Generic.List<Int32> ProductWishListIDsToDelete = new System.Collections.Generic.List<Int32>();
            foreach (GridViewRow row in GridViewProductWishList.Rows)
            {
                CheckBox cb = (CheckBox)(row.FindControl("CheckBoxDelete"));
                if (cb != null && cb.Checked)
                {
                    Int32 ProductId = (Int32)GridViewProductWishList.DataKeys[row.RowIndex].Value;
                    ECommerce.DAL.CustomCollection<TblProductWishList> data = new ECommerce.DAL.CustomCollection<TblProductWishList>();
                    data = TblProductWishListExtended.GetTblProductWishListByProductAndSystemUsertId(ProductId, SessionManager.LoggedUser.UserId);
                    TblProductWishList.DeleteTblProductWishList(data[0].ProductWishListId);
                }
            }
            LoadGrid();
            //GridViewProductWishList.DataBind();
        }
    }
    protected void GridViewProductWishList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header))
        {
            //adding an attribut for onclick event on the check box in the hearder and passing the ClientID of the Select All checkbox
            //((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onclick", "SelectAll('" + GridViewCatagory.ClientID + "','" + ((CheckBox)e.Row.FindControl("chkSelectAll")).ClientID + "')");
            //((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onclick", "SelectAll('" + GridViewProductWishList.ClientID + "','" + ((CheckBox)e.Row.FindControl("chkSelectAll")).ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
        }
    }


}